//
//  ConfigureServerViewController.m
//  ws-mgmt-client
//
//  Created by Cliff Duffey on 9/14/09. Mage Labs.
//

#import "ConfigureServerViewController.h"
#import "ConfigureItemViewController.h"
#import "ws_mgmt_clientAppDelegate.h"
#import "RootViewController.h"

@implementation ConfigureServerViewController
@synthesize url;
@synthesize name;
@synthesize username;
@synthesize password;
@synthesize listIndex;
@synthesize isNewServer;

#pragma mark Utility functions

//updates the server in the main server list with the new config items
- (void) save
{
	ws_mgmt_clientAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	NSArray *viewArray = [delegate.navigationController viewControllers];
	RootViewController *rootViewController = (RootViewController *)[viewArray objectAtIndex: 0];
	
	NSMutableDictionary *newServer = [[NSMutableDictionary alloc] initWithObjectsAndKeys: url, @"URL",
																						  name, @"Name",
																						  username, @"Username",
																						  password, @"Password", nil];
	//Add the current server to the server list
	[rootViewController addServer:newServer atIndex:listIndex];
	[rootViewController.tableView reloadData];
	[newServer release];
}

#pragma mark View related functions
/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSLog(@"In ConfigureServerViewController");
	
	self.title = @"Server Config";
	
    //Set up initial test data for the table
	if( self.isNewServer ) {
		self.url = @"URL";
		self.name = @"Name";
		self.username = @"Username";
		self.password = @"Password";
	}

}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}
*/

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"ConfigServerCell";
    
	int length = [self.password length];
	int i;

	NSMutableString *hiddenPassword = [NSMutableString stringWithCapacity: length];
	
	for (i = 0; i < length; i++)
		[hiddenPassword appendString: @"*"];
	
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
	
	// Create hidden password
    
    // Set up the cell...
	switch ([indexPath row]) {
		case 0:
			cell.textLabel.text = name; 
			cell.detailTextLabel.text = @"WinRM Server Name";
			break;
		case 1:
			cell.textLabel.text = url; 
			cell.detailTextLabel.text = @"WinRM Server URL";
			break;
		case 2:
			cell.textLabel.text = username; 
			cell.detailTextLabel.text = @"User ID";
			break;
		case 3:
			cell.textLabel.text = hiddenPassword;
			cell.detailTextLabel.text = @"Password";
			break;
		default:
			NSLog(@"Got a problem in setting up the cells for the Conf Serv");
			break;
	}
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic to push the ConfigureItemViewContoller view controller.
	ConfigureItemViewController *configureItemViewController = [[ConfigureItemViewController alloc] initWithNibName:@"ConfigureItemViewController" bundle:nil];
	
	//Determine which config item was selected and set the Config Item label accordingly
	NSString *configItemString;

	switch ([indexPath row]) {
		case 0:
			configItemString = [[NSString alloc] initWithString: @"Name"];
			configureItemViewController.labelText = configItemString; 
			[configItemString release];
			break;
		case 1:
			configItemString = [[NSString alloc] initWithString: @"URL"];
			configureItemViewController.labelText = configItemString; 
			[configItemString release];
			break;
		case 2:
			configItemString = [[NSString alloc] initWithString: @"Username"];
			configureItemViewController.labelText = configItemString; 
			[configItemString release];
			break;
		case 3:
			configItemString = [[NSString alloc] initWithString: @"Password"];
			configureItemViewController.labelText = configItemString; 
			[configItemString release];
			break;
		default:
			NSLog(@"Weird row selected in Configure Server View");
			break;
	}
	
	ws_mgmt_clientAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.navigationController pushViewController:configureItemViewController animated:YES]; 
	
	[configureItemViewController release]; 
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[url release];
	[name release];
	[username release];
	[password release];
	
    [super dealloc];
}


@end

